package org.wxy.sdk.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;

import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;

import org.wxy.sdk.Constants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Calendar;


/**
 * Created by wxy on 2018/05/27.
 *
 * @author wxy
 * @date on 2018/05/27 11:32
 */
public class AppLog {

    public static void init(Context context) {
        Logger.init(Constants.APP_LOG_TAG).logLevel(isApkDebugable(context) ? LogLevel.FULL : LogLevel.NONE).methodCount(5);
//        Logger.init(Constants.APP_LOG_TAG).logLevel(isApkDebugable(context) ? LogLevel.FULL : LogLevel.FULL).methodCount(5);
    }

    public static void d(String msg, Object...objects) {
        Logger.d(msg, objects);
    }

    public static void i(String msg, Object...objects) {
        Logger.i(msg, objects);
    }

    public static void i() {
        i("Call: AppLog.i()");
    }

    public static void e(String message, Throwable throwable) {
        Logger.e(throwable, message);
    }

    public static void e(Throwable throwable) {
        Logger.e(throwable, throwable.getMessage());
//        e(throwable.getMessage(), throwable);
    }

    public static void json(Object object) {
        try {
            String strJson = Constants.json().toJson(object);
            Logger.json(strJson);
        } catch (Exception e) {
            e(e.getMessage(), e);
        }
    }

    /**
     *
     * @param context
     * @param throwable
     */
    public static void file(Context context, Throwable throwable) {
        File file = new File(context.getExternalCacheDir(), "app_err.log");
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            String s = "-----------------------------------------------------------------\r\n" +
                    Calendar.getInstance().getTime().toString() +
                    "\r\n" +
                    (throwable == null ? "null_throwable" : throwable.getMessage()) +
                    "\r\n" +
                    "-----------------------------------------------------------------\r\n";
            fos.write(s.getBytes("UTF-8"));
            if (throwable != null) {
                throwable.printStackTrace(new PrintStream(fos));
                fos.write("\r\n-----------------------------------------------------------------\r\n".getBytes("UTF-8"));
            }
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 但是当我们没在AndroidManifest.xml中设置其debug属性时:
     * 使用Eclipse运行这种方式打包时其debug属性为true,使用Eclipse导出这种方式打包时其debug属性为法false.
     * 在使用ant打包时，其值就取决于ant的打包参数是release还是debug.
     * 因此在AndroidMainifest.xml中最好不设置android:debuggable属性置，而是由打包方式来决定其值.
     *
     * @param context
     * @return
     * @author SHANHY
     * @date   2015-8-7
     */
    public static boolean isApkDebugable(Context context) {
        try {
            ApplicationInfo info= context.getApplicationInfo();
            return (info.flags& ApplicationInfo.FLAG_DEBUGGABLE)!=0;
        } catch (Exception e) {
            e(e);
        }
        return false;
    }
}
